
#import "XDScrollView.h"

@interface XDScrollView()<UIScrollViewDelegate>
/**图片数组*/
@property (nonatomic,strong) NSMutableArray  *imageArray;
/**定时器*/
@property (nonatomic, strong)NSTimer *timer;
/**显示框架*/
@property(nonatomic,strong)UIImageView  *imageView;
@end

@implementation XDScrollView
//自定义初始化
- (instancetype)initWithFrame:(CGRect)frame  imageArray:(NSArray *)imageArray{
    if ([super  initWithFrame:frame]) {
        self.imageArray =[NSMutableArray  arrayWithArray:imageArray];
    }
    return self;
}

#pragma mark -------------懒加载--------------

- (UIScrollView *)scrollView
{
       if (!_scrollView) {
           self.scrollView = [[UIScrollView  alloc] initWithFrame:CGRectMake(0,64, WIDTH, HEIGHT/3)];
           
            //添加相册到scrollView上
            for (int i = 0; i < self.imageArray.count; i++) {
                
                self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH *i, 0, WIDTH, HEIGHT/3)];
                 [self.imageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[i]]];
                    [_scrollView addSubview:self.imageView];
                }
           
            //设置scrollView属性
            _scrollView.contentSize = CGSizeMake(WIDTH * self.imageArray.count, HEIGHT/3);
            _scrollView.pagingEnabled = YES;
            _scrollView.showsHorizontalScrollIndicator = NO;
            _scrollView.delegate = self;
           //回弹效果
            _scrollView.bounces  = NO;
        
           [self scrollViewWillBeginDragging:_scrollView ];
        }
        return _scrollView;
}

- (UIPageControl *)pageControl{
    
    if (!_pageControl) {
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(self.center.x,260, 0, 30)];
        self.pageControl.pageIndicatorTintColor = [UIColor blackColor];
        self.pageControl.numberOfPages = self.imageArray.count ;
    }
    return _pageControl ;
}

#define mark   ----------------scrollView 的代理方法--------------------

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //计算当前的页数和偏移量
    self.pageControl.currentPage = (scrollView.contentOffset.x / WIDTH );
    CGPoint offset = self.scrollView.contentOffset ;
    //  算出偏移多少个图片
    int pageCount = offset.x / WIDTH;
    // 重新给点赋值
    self.pageControl.currentPage = pageCount;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
        self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    [self.timer invalidate];
    self.timer = nil;
}
#pragma mark  ----------------定时器方法-----------------
- (void)nextPage{
    
    NSInteger nextPage = self.pageControl.currentPage + 1;
    if (nextPage == self.pageControl.numberOfPages) {
        nextPage = 0;
    }
    
    CGPoint offPoint = self.scrollView.contentOffset;
    offPoint.x = nextPage * WIDTH;
    [self.scrollView setContentOffset:offPoint animated:YES];
}
@end
